﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Mime
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Text;

    using Mews.IO;

    /// <summary>Processes an unstructured header field that might contain encoded words.</summary>
    internal sealed class DecodingUnstructuredHeaderFieldBodyProcessor : HeaderFieldBodyProcessor
    {
        // The following field must not be readonly, otherwise method calls are unable to update private fields of the
        // EncodedWordDecoder struct.
        private EncodedWordDecoder decoder = new EncodedWordDecoder(75, 16);
        private readonly UnstructuredHeaderField result;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal DecodingUnstructuredHeaderFieldBodyProcessor(string fieldName)
        {
            this.result = new UnstructuredHeaderField(fieldName);
        }

        internal sealed override void ProcessNormalBodyEvent(byte ev)
        {
            this.decoder.ProcessEvent(ev, false);
        }

        internal sealed override void ProcessQuotedBodyEvent(byte ev, bool isQuoteChar)
        {
            this.decoder.ProcessEvent(ev, false); // Quoting in unstructured fields must not be observed
        }

        internal sealed override void ProcessCommentBodyEvent(byte ev)
        {
            this.decoder.ProcessEvent(ev, false);
        }

        internal sealed override HeaderField GetHeaderField()
        {
            this.result.Body = this.decoder.GetResult();
            return this.result;
        }
    }
}
